<?php
/**
 * *******************************************************************************
 * ImportedFiles Interface - Action 07
 * Import a File as new document and save attributes values
 * *******************************************************************************
 * @author $Author: damien.viguier@gmail.com $
 * @version $Rev: 33 $
 * @internal $Id: ajx-ui-ImportedFiles-a07.php 33 2013-08-25 15:15:32Z damien.viguier@gmail.com $
 * @internal $LastChangedDate: 2013-08-25 15:15:32 +0000 (Sun, 25 Aug 2013) $
 * @link $HeadURL: http://personal-ged.googlecode.com/svn/trunk/webroot/ajax/data/ajx-ui-ImportedFiles/ajx-ui-ImportedFiles-a07.php $
 * *******************************************************************************
 */

include_once './../class/DocTypes.class.php';
include_once './../class/ImportedFiles.class.php';
include_once './../../../lib/AttributesDefManagement.class.php';
error_reporting(E_ERROR);

/* ************************************* */
//print_r($_REQUEST);
$l_b_RequestValid = true;
$l_s_docType="";

if(array_key_exists('DocTypeID', $_REQUEST) && array_key_exists('FILE_IMP_ID', $_REQUEST))
{
	$l_s_docType = $_REQUEST['DocTypeID'];
	$l_as_results = DocTypes::getAllAttributesDefByDocTypeID($l_s_docType);

	$l_s_results["infos"] = "";
	
	if(count($l_as_results)==0)
	{
		$l_s_results["noresults"] = true;
		$l_s_results["err"] = "<br>No Attribute for Document type - Import of document canceled !";
		$l_b_RequestValid = false;
	}
	else {
		// Validation of mandatories HTTP Fields
		foreach($l_as_results as $l_s_key => $l_as_row)
		{
			/**
			 * DOCTYPE_CODE,
					DOCTYPEATTRDEF_ID,
					ATTR_LNGLABEL,
					ATTR_SHTLABEL,
					ATTR_IS_MULTIPLE,
					ATTR_IS_FROM_LIST,
					ATTR_IS_FREE,
					ATTR_IS_MANDATORY,
					ATTR_FORMAT,
					ATTR_FORMAT_PATTERN,
					LST_CODE,
					LST_VALUES_CNT
			 */
			$l_s_id = $l_as_row['DOCTYPE_CODE']."_".$l_as_row['DOCTYPEATTRDEF_ID'];
			if($l_as_row['ATTR_IS_MANDATORY']==1 && !array_key_exists($l_s_id, $_REQUEST))
			{
				$l_b_RequestValid = false;
				$l_s_results["err"] = "<br>Mandatory field missing !";
				$l_s_results["noresults"] = true;
			}
		}	
		
		
	}
}
else
{
	$l_s_results["err"] = "No document type identified.";
	$l_s_results["noresults"] = true;
	$l_b_RequestValid = false;
}

if($l_b_RequestValid)
{
	$l_s_results["infos"] .= "<br>- Manadatory Fields OK";
	try{
		$l_b_trans = DBManagement::beginTransaction();
		
		if(!$l_b_trans)
		{
			throw new Exception("Error during db transaction initialization !");
		}
		else
		{
			$l_s_results["infos"] .= "<br>- DB transaction opened successfully !";
		}
		// Register Document
		$l_i_idDoc = ImportedFiles::importFileToDocument($_REQUEST['FILE_IMP_ID'], $l_s_docType, null);
		$l_s_results["infos"] .= "<br>- Document created with id : ".$l_s_docType."-".$l_i_idDoc;
		
		// Attributes Values
		foreach($l_as_results as $l_s_key => $l_as_row)
		{
			/**
			 * DOCTYPE_CODE,
			 	DOCTYPEATTRDEF_ID,
			 	ATTR_LNGLABEL,
			 	ATTR_SHTLABEL,
			 	ATTR_IS_MULTIPLE,
			 	ATTR_IS_FROM_LIST,
				 ATTR_IS_FREE,
				 ATTR_IS_MANDATORY,
			 	ATTR_FORMAT,
			 	LST_CODE,
			 	LST_VALUES_CNT
			 */
			$l_s_id = $l_as_row['DOCTYPE_CODE']."_".$l_as_row['DOCTYPEATTRDEF_ID'];
			
			//$l_s_results["infos"] .= "<br>- Before setting Attribute with ".$l_i_idDoc." , ".$l_s_docType." , ".$l_as_row['DOCTYPEATTRDEF_ID'].", ".$_REQUEST[$l_s_id];
			$l_i_attrId = ImportedFiles::addSimpleAttrValueToDocument($l_i_idDoc, $l_s_docType, $l_as_row['DOCTYPEATTRDEF_ID'], $_REQUEST[$l_s_id]);
			//$l_s_results["infos"] .= "<br>- After setting Attribute with ".$l_i_idDoc." , ".$l_s_docType." , ".$l_as_row['DOCTYPEATTRDEF_ID'].", ".$_REQUEST[$l_s_id];
			if($l_as_row['ATTR_IS_FROM_LIST'] ==1 && $l_as_row['LST_CODE'] != "" )
			{
				//$l_s_results["infos"] .= "<br>- Before setting ListValued Attribute with ".$l_i_idDoc." , ".$l_as_row['LST_CODE'].", ".$l_as_row['DOCTYPEATTRDEF_ID'].",".$l_i_attrId.", ".$_REQUEST[$l_s_id];
				ImportedFiles::addListedAttrValueToDocument($l_i_idDoc, $l_as_row['LST_CODE'], $l_as_row['DOCTYPEATTRDEF_ID'],$l_i_attrId, $_REQUEST[$l_s_id]);
			}
			$l_s_results["infos"] .= "<br>- Attribute '".$l_s_id."' defined : ".$_REQUEST[$l_s_id];
		}
		DBManagement::commitTransaction();
	}
	catch(PDOException $p_o_pdoException)
	{
		DBManagement::rollbackTransaction();
		$l_s_results["err"] = $p_o_pdoException->getMessage()."<br><strong> Transaction Rollbacked </strong><BR> - Status Infos : <BR>".$l_s_results["infos"];
		$l_s_results["noresults"] = true;
	}
	catch(Exception $p_o_e)
	{
		$l_s_results["err"] = "<br><strong>".$p_o_e->getMessage()."</strong><BR> - Status Infos : <BR>".$l_s_results["infos"];
		$l_s_results["noresults"] = true;
	}
	
}
// Callback for request
echo json_encode($l_s_results);

?>